﻿using CXW.Core.Sharp.Sda;
using DevExpress.XtraEditors;
using System;
using System.Windows.Forms;

namespace CXW.Core.Sharp.Services
{
    public class WinFormsMessageService : IDialogMessageService
    {
        public IWin32Window DialogOwner { get; set; }

        DialogResult ShowMessageBox(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
        {
            return XtraMessageBox.Show(DialogOwner, text, ResourceService.GetString(caption), buttons, icon, defaultButton);
        }
        /// <summary>确认对话框,是返回 true,否则返回 false</summary>
        /// <param name="text">要在消息框中显示的文本</param>
        /// <param name="caption">要在消息框标题栏中显示的文本,值为null时显示"确认"</param>
        /// <returns>是返回 true,否则返回 false</returns>
        public bool AskQuestion(string text, string caption = null)
        {
            return DialogResult.Yes == ShowMessageBox(text, caption == null ? "Info.CaptionConfirm" : caption, MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        }
        /// <summary>显示信息</summary>
        /// <param name="text">要在消息框中显示的文本</param>
        /// <param name="caption">要在消息框标题栏中显示的文本,值为null时显示"提示"</param>
        public void ShowMessage(string text, string caption = null)
        {
            ShowMessageBox(text, caption == null ? "Info.CaptionInformation" : caption, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        }
        /// <summary>显示警告信息</summary>
        /// <param name="text">要在消息框中显示的文本</param>
        /// <param name="caption">要在消息框标题栏中显示的文本,值为null时显示"警告"</param>
        public void ShowWarning(string text, string caption = null)
        {
            ShowMessageBox(text, caption == null ? "Info.CaptionWarning" : caption, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
        }
        /// <summary>显示错误信息,消息对话框包含"重试"和"取消"两个按钮,点击"重试"按钮时,返回 true,否则返回 false</summary>
        /// <param name="text">要在消息框中显示的文本</param>
        /// <param name="caption">要在消息框标题栏中显示的文本,值为null时显示"错误"</param>
        /// <returns>点击"重试"按钮时,返回 true,否则返回 false</returns>
        public bool ShowError(string text, string caption = null)
        {
            return DialogResult.Retry == ShowMessageBox(text, caption == null ? "Info.CaptionError" : caption, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        }

        public bool ShowException(Exception ex, string message)
        {
            if (ex != null)
            {
                ExceptionBox.ShowErrorBox(ex, message);
                return default(bool);
            }
            else
                return ShowError(message);
        }
    }
}
